package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.consts.MoldReplacePartType;
import com.sugon.dao.ModuleReplacePartDao;
import com.sugon.entity.ModuleReplacePartEntity;
import com.sugon.modules.mold.consts.MoldReplacePartProgress;
import com.sugon.service.ModuleReplacePartService;
import com.sugon.service.SysMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 模具-备件更换记录Service实现类
 *
 * @author sugon
 * @date 2019-08-01 16:07:32
 */
@Service("moduleReplacePartService")
public class ModuleReplacePartServiceImpl implements ModuleReplacePartService {

    @Autowired
    private ModuleReplacePartDao moduleReplacePartDao;
    @Autowired
    private SysMsgService sysMsgService;

    /**
     * @param id 主键
     * @return
     */
    @Override
    public ModuleReplacePartEntity queryObject(String id) {
        return moduleReplacePartDao.queryObject(id);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<ModuleReplacePartEntity> queryList(Map<String, Object> map) {
        return moduleReplacePartDao.queryList(map);
    }

    /**
     * @param moldId      模具ID
     * @param typeMethod
     * @param replaceCode
     * @return
     */
    @Override
    public List<ModuleReplacePartEntity> queryList(String moldId, Integer typeMethod, String replaceCode) {
        Map<String, Object> queryParam = MapUtil.newHashMap();
        queryParam.put("moldId", moldId);
        queryParam.put("typeMethod", typeMethod);
        queryParam.put("replaceCode", replaceCode);
        return queryList(queryParam);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleReplacePartDao.queryTotal(map);
    }

    /**
     * @param moduleReplacePart 实体
     * @return
     */
    @Override
    public int save(ModuleReplacePartEntity moduleReplacePart) {
        moduleReplacePart.setId(RandomUtil.randomString(32));
        moduleReplacePart.setCreateUser(getUserId());
        moduleReplacePart.setCreateTime(DateUtil.date());
        moduleReplacePart.setUpdateTime(moduleReplacePart.getCreateTime());
        moduleReplacePart.setUpdateUser(moduleReplacePart.getCreateUser());
        moduleReplacePart.setDelFlag(1);
        if (MoldReplacePartType.OTHER.getValue().equals(moduleReplacePart.getTypeMethod())) {
            moduleReplacePart.setProgress(MoldReplacePartProgress.DEPT_APPROVING);
        } else {
            moduleReplacePart.setProgress(MoldReplacePartProgress.NEW);
        }
        return moduleReplacePartDao.save(moduleReplacePart);
    }

    /**
     * @param moduleReplacePart 实体
     * @return
     */
    @Override
    public int update(ModuleReplacePartEntity moduleReplacePart) {
        moduleReplacePart.setUpdateUser(getUserId());
        moduleReplacePart.setUpdateTime(DateUtil.date());
        int rows = moduleReplacePartDao.update(moduleReplacePart);
        if (rows > 0) {
            sysMsgService.addMoldReplacePartApproveItem(moduleReplacePart.getId(), 0);
        }
        return rows;
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return moduleReplacePartDao.delete(id);
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return moduleReplacePartDao.deleteBatch(ids);
    }

}
